<?php
/**
 * File description.
 * 
 * @filesource
 * @author Michael N <webjestic@gmail.comt>
 * @link http://webjestic.net webJestic
 * @license http://www.opensource.org/licenses/bsd-license.php BSD
 * @copyright Copyright (c) 2009 - webJestic.NET.  All Rights Reserved.
 * 
 * 
 * @category   Engine
 * @package    Resources
 * @subpackage FrontController 
 */


class Engine_Resource_Front extends Zend_Application_Resource_ResourceAbstract {
	
	protected $_front;
	protected $cache;
	
	public function init() {	
		$this->cache = Zend_Registry::get('Zend_Cache');
		return $this->getFrontController();
	}
	
	
	public function getFrontController() {
		if (null === $this->_front) {
			$this->_front = Zend_Controller_Front::getInstance();
			$this->_front->throwExceptions(false);
			
			$this->loadModules();
			$this->loadPlugins();
			$this->loadHelpers();
			
			
			// TODO depreciate - remove
			foreach ($this->getOptions() as $key => $value) {
				switch($key) {
					case 'moduleControllerDirectoryName' :
						$this->_front->setModuleControllerDirectoryName($value);
						break;
					case 'actionhelpers' :
							if (is_array($value)) {
	                        	foreach ($value as $helperPrefix => $helperPath) {
	                            	Zend_Controller_Action_HelperBroker::addPath($helperPath, $helperPrefix);
	                        }
	                    }
						break;
					case 'params' :
						$this->_front->setParam($key, $value);
						break;
					default :
						$this->_front->setParam($key, $value);
						break;
				}
			}
		}		
		
		return $this->_front;
	}
	

	protected function loadModules() {
		// Load Modules
		if( ($results = $this->cache->load('core_modules')) === false ) {		
			$core_modules = new Engine_Db_Table_CoreModules();
			$select = $core_modules->select()->where('module_active = ?', true);
			$results = $core_modules->fetchAll($select);		
			$this->cache->save($results, 'core_modules');
		}
		foreach($results as $row) {
			$this->_front->addControllerDirectory((APPLICATION_PATH."/modules/".$row['module_name']."/controllers"), $row['module_name']);
		}		
	}
	
	protected function loadPlugins() {
		
		// Load all FIRST run Engine Plugins
		$pluginOptions = array();
		$plugin = new Engine_Controller_Plugin_Blacklist($pluginOptions);
		$this->_front->registerPlugin($plugin);
		$plugin = new Engine_Controller_Plugin_Routes($pluginOptions);
		$this->_front->registerPlugin($plugin);
		$plugin = new Engine_Controller_Plugin_Acl($pluginOptions);
		$this->_front->registerPlugin($plugin);
		
		// Load WJ_Plugins
		if( ($results = $this->cache->load('core_plugins')) === false ) {
			$core_plugins = new Engine_Db_Table_CorePlugins();
			$select = $core_plugins->select()->where('plugin_active = TRUE');
			$results = $core_plugins->fetchAll($select);
			$this->cache->save($results, 'core_plugins');
		}
		foreach($results as $row) {
			$pluginOptions = array();
			$pluginOptions = unserialize($row['plugin_options']);
			$plugin = new $row['plugin_classname']($pluginOptions);
			$this->_front->registerPlugin($plugin);
		}		
		
		// Load all LAST run Engine Plugins
		$pluginOptions = array();
		$plugin = new Engine_Controller_Plugin_FinalFilter($pluginOptions);
		$this->_front->registerPlugin($plugin);
	}
	
	protected function loadHelpers() {
		
	}
	
}

?>